/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package attackDetection;
import java.sql.*;

/**
 *
 * @author john
 */
public class Suspicious {
    private Storage.DBConnect db;
    public Suspicious(){}
    
    public Suspicious(int nodeId){
        db = new Storage.DBConnect();
        db.connectToDatabase();
        db.executeQuery("INSERT INTO suspicious (nodeid,level) VALUES (" + nodeId + ",0)", false);
        db.closeConnections();
    }
    
    public void increase(int nodeId, String attackId){
        db = new Storage.DBConnect();
        db.connectToDatabase();
        String attackName = "";
        
        if(!db.executeQuery("SELECT * FROM suspicious WHERE nodeid=" + nodeId + "", true))
            db.executeQuery("INSERT INTO suspicious (nodeid, attackid, level) VALUES (" + nodeId +"," + attackId +"," + "1)", false);
        else{
            try {
                attackName = db.getResultSet().getString("attackid");
            } catch (Exception e){
                System.out.println(e.getMessage());
            }
            if(!attackName.equalsIgnoreCase(attackId))
                db.executeQuery("UPDATE suspicious SET level = level +1, attackid = multiple WHERE suspicious.nodeid=" + nodeId +" LIMIT 1", true);
            else
                db.executeQuery("UPDATE suspicious SET level=level+1 WHERE nodeid=" + nodeId + " LIMIT 1", true);
        }
        
        db.closeConnections();
    }
}
